
package com.umeox.phone.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;

import com.umeox.phone.ui.InCallUIActivity;
import com.umeox.phone.util.CacheUtils;
import com.umeox.phone.util.Utils;
import com.umeox.watch.moto.dataservice.constants.Constants;

public class OutCallReceiver extends BroadcastReceiver {

    static final String TAG = "OutCallReceiver";

    public static final String NETWORK_NOT_AVAILABLE = "com.wherecom.intent.action.MOBILE_NETWORK_NOT_AVAILABLE";
    /**
     * 拨出号码
     * 对方已挂断
     */
    public static final String CALL_DISCONNECTED = "android.intent.action.MO_DISCONNETCTED";
    /**
     * 拨出号码
     * 对方已接听
     */
    public static final String CALL_CONNECTED = "android.intent.action.MO_CONNETCTED";
    /**
     * 监听来电广播
     *
     *
     */
    public static final String ACTION_PHONE_STATE_CHANGED =
            "android.intent.action.PHONE_STATE";





    String phoneNum;
    private static long offhookTime;

    private static final Object monitor = new Object();

    @Override
    public void onReceive(Context context, Intent intent) {
        final Context ctx = context;

        String action = intent.getAction();

        if (action == null) {
            return;
        }

        phoneNum = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);//拨出号码
        boolean isListen = CacheUtils.isCallMonitoring(ctx);
        Log.d(TAG, "OutCallReceiver&onReceive: " + action + " isListen:" + isListen);

        switch (action) {
            case Intent.ACTION_NEW_OUTGOING_CALL:
                if (isListen) {
                    Utils.closeSpeaker(ctx);
                    return;
                }

                showActivity(ctx, phoneNum);
                break;
            case CALL_CONNECTED: //去电接通
                if (isListen) {
                    return;
                }
                offhookTime = System.currentTimeMillis();
                Utils.sendOutCallConnectBroadCast(ctx);
                break;

            case CALL_DISCONNECTED:
                //挂断
                if (isListen) {
                    CacheUtils.setCallMonitoring(ctx, false);
                    Utils.sendListenCallEndBroadCast(ctx);
                    return;
                }

                //此处不注释会导致上报2次通话定位
//                Utils.sendEndCallBroadCast(ctx, 0, offhookTime);
                break;
            case NETWORK_NOT_AVAILABLE:
                //网络不可用 通知界面显示Toast 并退出拨打界面
                new Handler().postDelayed(() -> ctx.sendBroadcast(new Intent("com.wherecom.intent.inner.action.MOBILE_NETWORK_NOT_AVAILABLE")), 2000);
                break;
            case Constants.ACTION_CALL_PHONE:
                if (Utils.isPhoneIdle(context)) {
                    CacheUtils.setCallMonitoring(context, true);
                    String phoneNumber = intent.getStringExtra(Constants.EXTRA_PHONE_NUMBER);
                    Intent intent1 = new Intent();
                    intent1.setAction(Intent.ACTION_CALL);
                    intent1.setData(Uri.parse("tel:" + phoneNumber));
                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent1);
                }
                break;

        }
    }


    private void showActivity(Context ctx, String number) {

        Intent intent = new Intent(ctx, InCallUIActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(InCallUIActivity.CALL_TYPE, 1);
        intent.putExtra(InCallUIActivity.EXTRA_PHONE_NUM, number);
        ctx.startActivity(intent);
    }
}
